import { defineConfig, loadEnv } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import packagePlugin from './vite-plugin-package-dist'
import { version as appVersion } from './package.json'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())

  return {
    base: env.VITE_CONTEXT_PATH + '/',
    plugins: [
      vue(),
      env.VITE_ENABLE_DIST_PACKAGE === 'true' &&
        packagePlugin(`${env.VITE_DEST_PACKAGE_OUT_DIR}/${env.VITE_APP_KEY}-web-${appVersion}.zip`)
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    build: {
      rollupOptions: {
        manualChunks: {
          vue: ['vue'],
          'element-plus': ['element-plus']
        }
      },
      chunkSizeWarningLimit: 1000
    },
    server: {
      host: true,
      port: 3001,
      proxy: {
        [env.VITE_API_PREFIX]: {
          target: env.VITE_API_ORIGIN,
          changeOrigin: true
        }
      }
    }
  }
})
